package com.roc.steelend.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.tiaopiao.FindTiaoPiaoReception;
import com.roc.steelend.controller.vo.tiaopiao.TiaoPiaoSaleReception;
import com.roc.steelend.mapper.ClientMapper;
import com.roc.steelend.mapper.SaleOrderMapper;
import com.roc.steelend.mapper.TiaoPiaoSaleMapper;
import com.roc.steelend.pojo.Client;
import com.roc.steelend.pojo.TiaoPiaoBuy;
import com.roc.steelend.pojo.TiaoPiaoSale;
import com.roc.steelend.service.TiaoPiaoSaleService;
import com.roc.steelend.util.LshUtil;
import io.swagger.annotations.Authorization;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class TiaoPiaoSaleServiceImpl  implements TiaoPiaoSaleService {


    @Autowired
    private TiaoPiaoSaleMapper tiaoPiaoSaleMapper;
    @Autowired
    private SaleOrderMapper saleOrderMapper;
    @Autowired
    private ClientMapper clientMapper;

    @Override
    public ReturnMsg postTiaoPiaoBySale(TiaoPiaoSaleReception tiaoPiaoSaleReception, Integer employeeId) {

        ReturnMsg msg = new ReturnMsg();
        TiaoPiaoSale tiaoPiaoSale = new TiaoPiaoSale();
        BigInteger tiaoPiaoSaleLsh = LshUtil.getTiaoPiaoSale();

        tiaoPiaoSale.setTiaoPiaoSaleId(tiaoPiaoSaleLsh);
        tiaoPiaoSale.setOperateTime(new Timestamp(new Date().getTime()));
        tiaoPiaoSale.setTiaoPiaoSaleTime(tiaoPiaoSaleReception.getTiaoPiaoTime());
        tiaoPiaoSale.setEmployeeId(employeeId);
        tiaoPiaoSale.setClientId(tiaoPiaoSaleReception.getClientId());
        tiaoPiaoSale.setOriginallyInvoice(tiaoPiaoSaleReception.getOldInvoice());
        tiaoPiaoSale.setNowInvoice(tiaoPiaoSaleReception.getNewInvoice());
        if (Objects.equals(tiaoPiaoSaleReception.getRemark(), "")){
            tiaoPiaoSaleReception.setRemark(null);
        }
        tiaoPiaoSale.setRemark(tiaoPiaoSaleReception.getRemark());
        int insert = tiaoPiaoSaleMapper.insert(tiaoPiaoSale);
        if (insert!=1){
            msg.setCode(0);
            msg.setMsg("调票操作失败，请重试");
            return  msg;
        }

        QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<>();

        clientQueryWrapper.eq("client_id",tiaoPiaoSaleReception.getClientId());
        Client client = clientMapper.selectList(clientQueryWrapper).get(0);
        client.setInvoice(tiaoPiaoSaleReception.getNewInvoice());
        clientMapper.updateById(client);

        saleOrderMapper.updateTiaoPiaoByClient(tiaoPiaoSaleReception.getClientId(), tiaoPiaoSaleLsh);

        msg.setMsg("调票操作成功");
        msg.setCode(1);
        return msg;
    }

    @Override
    public List<HashMap<String, Object>> findTiaoPiaoDataBySale(FindTiaoPiaoReception findTiaoPiaoReception) {
        Integer now = findTiaoPiaoReception.getCurrentPage();
        Integer size = findTiaoPiaoReception.getPageSize();
        now=(now-1)*size;
        String duiXiang = findTiaoPiaoReception.getDuiXiang();
        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }
        return tiaoPiaoSaleMapper.findTiaoPiaoDataBySale(now,size,duiXiang);
    }

    @Override
    public Integer findTiaoPiaoDataBySaleCount(FindTiaoPiaoReception findTiaoPiaoReception) {
        String duiXiang = findTiaoPiaoReception.getDuiXiang();
        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }

        return tiaoPiaoSaleMapper.findTiaoPiaoDataBySaleCount(duiXiang);
    }

    @Override
    public List<TiaoPiaoSale> saleTiaoPiaoDetailSelect(BigInteger tiaoPiaoId) {
        QueryWrapper<TiaoPiaoSale> tiaoPiaoSaleQueryWrapper = new QueryWrapper<>();
        tiaoPiaoSaleQueryWrapper.eq("tiao_piao_sale_id",tiaoPiaoId);

        return  tiaoPiaoSaleMapper.selectList(tiaoPiaoSaleQueryWrapper);
    }
}
